package com.tuling.netty.splitpacket;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

public class SpiltPacketClientHandler extends SimpleChannelInboundHandler {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        SocketAddress socketAddress = ctx.channel().remoteAddress();
        System.out.println("接收到远程服务端的地址是"+socketAddress);
        Channel channel = ctx.channel();
        String message = "浙江大学&……%*%￥#@！";
        //连接之后向服务端发送多次请求
        for (int i = 0; i < 30; i++) {
            MyMessagePropol propol = new MyMessagePropol();
            propol.setLen(message.getBytes(StandardCharsets.UTF_8).length);
            propol.setContent(message.getBytes(StandardCharsets.UTF_8));
            channel.writeAndFlush(propol);
        }
    }



    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        //接收到服务端的信息是
        System.out.println("接收到服务端的信息是"+msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
